package com.example.content_provider_client;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.content_provider_client.constant.UserInfoContent;
import com.example.content_provider_client.entity.UserInfo;
import com.example.content_provider_client.utils.ToastHelper;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etUserName;
    private EditText etUserAge;
    private EditText etUserHeight;
    private EditText viewById;
    private EditText etUserWeight;
    private CheckBox cbUserMarried;
    private Button btnSave;
    private Button btnRead;
    private Button btnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_info);


        etUserName = findViewById(R.id.et_user_name);
        etUserAge = findViewById(R.id.et_user_age);
        viewById = findViewById(R.id.et_user_height);
        etUserHeight = viewById;
        etUserWeight = findViewById(R.id.et_user_weight);
        cbUserMarried = findViewById(R.id.cb_user_married);

        btnSave = findViewById(R.id.btn_save);
        btnRead = findViewById(R.id.btn_read);
        btnDelete = findViewById(R.id.btn_delete);

        btnSave.setOnClickListener(this);
        btnRead.setOnClickListener(this);
        btnDelete.setOnClickListener(this);

    }

    private UserInfo getUserInfo() {
        UserInfo user = new UserInfo();
        user.name = etUserName.getText().toString();
        user.age = Integer.valueOf(etUserAge.getText().toString());
        user.height = Integer.valueOf(etUserHeight.getText().toString());
        user.weight = Integer.valueOf(etUserWeight.getText().toString());
        user.married = cbUserMarried.isChecked();

        return user;
    }

    @SuppressLint("Range")
    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.btn_save) {
            ContentValues values = new ContentValues();
            values.put(UserInfoContent.USER_NAME, etUserName.getText().toString());
            values.put(UserInfoContent.USER_AGE, etUserAge.getText().toString());
            values.put(UserInfoContent.USER_HEIGHT, Long.parseLong(etUserHeight.getText().toString()));
            values.put(UserInfoContent.USER_WEIGHT, Float.parseFloat(etUserWeight.getText().toString()));
            values.put(UserInfoContent.USER_MARRIED, cbUserMarried.isChecked());

            // 使用ContentResolver获取ContentProvider 执行保存操作
            getContentResolver().insert(UserInfoContent.CONTENT_URI, values);

            ToastHelper.show(this, "保存成功");

        } else if (id == R.id.btn_read) {
            // 使用ContentResolver获取ContentProvider 执行查询操作
            Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null, null);

            if (cursor == null) return;

            while (cursor.moveToNext()) {
                UserInfo user = new UserInfo();
                user.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                user.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                user.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
                user.height = cursor.getLong(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
                user.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
                user.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED)) == 1 ? true : false;

                Log.d("zhoutao", user.toString());
            }
            cursor.close();

        } else if (id == R.id.btn_delete) {
            // 根据id删除，
            // 创建uri：com.example.content_provider_server.provider.UserContentProvider/user/12
//            Uri uri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI, 30);
//            Log.d("zhoutao", uri.toString());
//            getContentResolver().delete(uri, null, null);

            // 删除所有
            String[] whereArgs = {etUserName.getText().toString()};
            getContentResolver().delete(UserInfoContent.CONTENT_URI, "name = ? ", whereArgs);
            ToastHelper.show(this, "删除成功");
        }
    }
}